package com.example.springboottest.controller;

import com.example.filedate.vo.ImgRequestVo;
import com.example.filedate.service.ImageService;
import com.example.springboottest.config.ApiOperation;
import com.example.springboottest.config.OperationType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

@RestController
public class ImageController {

    @Resource
    private ImageService imageService;

    /**
     * 指定图片绘制文本文字
     */
    @OperationType("select")
    @ApiOperation(value = "获取图片")
    @PostMapping("/getImage")
    public void getImage(HttpServletResponse response,@RequestBody ImgRequestVo requestVo) throws IOException {
        BufferedImage imageWithText = imageService.getImage(requestVo);
        // 设置响应类型为图片
        response.setContentType("image/jpeg");
        // 将图片写入响应的输出流
        ImageIO.write(imageWithText, "jpeg", response.getOutputStream());
    }
}
